package br.com.wai2008.client.service;

import java.io.BufferedReader;
import java.io.IOException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.util.Date;

import br.com.wai2008.client.configurations.ClientConfigurations;
import br.com.wai2008.client.protocol.ApplyProtocol;
import br.com.wai2008.client.serial.HDSerialNumber;
import br.com.wai2008.client.wireless.WirelessSignal;
import br.com.wai2008.socket.client.SocketClient;

/**
 * This class implements the execution for a request to the server through
 * socket. It starts a connection to a specific port on server and executes the 
 * process of send and receive messages using a specific protocol.
 * 
 * @since 
 * 		September, 2008.
 * 
 * @version
 * 		WAI2008 - version 1.0
 * 
 * @author 
 * 		Andre Romero Goncales
 */
public class ServiceThread extends Thread {

	/**
	 * Indicates when we stop the requests to the server.
	 */
	private boolean request = true;

	/**
	 * Run state from thread. This method start a connection to the server, 
	 * in a specific port and send and receive messages to/from 
	 * the server.
	 * 
	 * @see
	 * 		br.com.wai2008.client.configurations.ClientConfigurations
	 */
	public void run() {
		System.out.println("[ServiceThread] Starting service...");
		while (this.request) {
			try {
				String message = this.buildMessage();
				if (message != null) {
					SocketClient socketClient = new SocketClient(ClientConfigurations.SERVER_HOST, 
							ClientConfigurations.SERVER_PORT, ClientConfigurations.REQUEST_TIMEOUT);
					socketClient.startConnection();
					System.out.println("DEBUG " + new Date().getMinutes() + ":" + new Date().getSeconds());
					socketClient.sendMessage(message);
					String serverResponse = socketClient.receiveMessage();
					System.out.println("DEBUG " + new Date().getMinutes() + ":" + new Date().getSeconds());
					//TODO process response
				} else {
					System.out.println("[ServiceThread] ERROR: Cannot build message");
				}
				Thread.sleep(ClientConfigurations.TIME_BETWEEN_MEASURE);
			} catch (UnknownHostException e) {
				System.out.println("[ServiceThread] ERROR: Server not found");
				e.printStackTrace();
				break;
			} catch (SocketTimeoutException e) { 
				System.out.println("[ServiceThread] ERROR: Timeout while waiting response message");
				e.printStackTrace();
			} catch (IOException e) {
				System.out.println("[ServiceThread] ERROR: Failed to send message");
				e.printStackTrace();
				break;
			} catch (InterruptedException e) {
				System.out.println("[ServiceThread] ERROR: Thread failed to sleep");
				e.printStackTrace();
			}
		}
	}

	/**
	 * Calls a service which build the message to be sent to the server.<br>
	 * In order to build the message, it calls system command's executions
	 * to get the HD serial number and wireless signals.
	 * 
	 * @see
	 * 		import br.com.wai2008.client.protocol.ApplyProtocol
	 * @see
	 * 		import br.com.wai2008.client.serial.HDSerialNumber
	 * @see
	 * 		import br.com.wai2008.client.wireless.WirelessSignal
	 * 
	 * @return
	 * 		Message in a specific protocol.
	 */
	private String buildMessage() {
		BufferedReader brIwlist = WirelessSignal.getSignal();
		BufferedReader brHDNumber = HDSerialNumber.getSerial();
		return ApplyProtocol.buildMessage(brHDNumber, brIwlist, "", 0, 0, "0");
	}

	/**
	 * Stop the requests to the server.
	 */
	public void stopRequest() {
		this.request = false;
	}
	
}
